#Author: Tanyong
# *-* coding:utf8 *-*
# @time       : 2018/7/17 21:57
# @Author     : Tanyong
# @Site       : 
# @File       : views.py
# @Software   : PyCharm
from flask import flash
from flask import redirect
from flask import render_template
from flask import url_for

from info import db
from info.models import Info, Focus
from . import index_blu

@index_blu.route('/')
def index():
    datainfo = Info.query.all()
    return render_template("home/index.html", datainfo=datainfo)

@index_blu.route("/add_focus/<code>")
def add_focus(code):
    stock = Info.query.filter(Info.code==code).first()
    if not stock:
        flash("没有这只股票,无法添加关注!!!")
    else:
        stock_focus = Focus.query.filter(Focus.info_id==stock.id).first()

        if stock_focus:
            flash("已经关注过该股票了, 请勿重复关注")
        else:
            focus = Focus(info_id=stock.id)
            try:
                db.session.add(focus)
                db.session.commit()
            except Exception as e:
                db.session.rollback()
                print(e)
                flash("添加关注失败!")

    return redirect(url_for("index.index"))
